import os
import click
from pyfiglet import Figlet
from colorama import Fore, Style, init
from six import print_
from termcolor import colored
from Shell.core.conversation import get_response
from Shell.core.executor import execute_command
from Shell.core.model import initialize_client
from Shell.utils.helpers import clear_screen, clear_history
from Shell.utils.config_manager import get_model_config


@click.command()
def terminal():
    init()
    f = Figlet(font='slant')
    print_(colored(f.renderText('AI Shell'), 'cyan'))

    # 从配置文件中获取模型名称
    model_config = get_model_config("Model")
    model_name = model_config.get("model_name", "pixtral-large-latest")

    # 初始化模型客户端
    model_client = initialize_client(model_name)

    while True:
        current_path = os.getcwd()
        user_input = input(f"{current_path}> " + Style.NORMAL)
        if user_input.lower() in ["exit", "quit"]:
            print(Fore.BLUE + "拜拜!")
            break
        elif user_input.lower() in ["clean", "清理", "-c"]:
            clear_screen()
            continue
        elif user_input.lower() in ["clear history", "清理历史", "-ch"]:
            clear_history()
            continue
        elif user_input.lower() in ["-help", "help", "帮助", "--help", "--h", "-h"]:
            print(Fore.BLUE + "欢迎使用AI Shell! ")
            print(Fore.GREEN + "可用命令:")
            print(Fore.YELLOW + "  -help / help: 显示帮助信息")
            print(Fore.YELLOW + "  exit / quit: 退出终端")
            print(Fore.YELLOW + "  clean / 清理 / -c: 清除屏幕")
            print(Fore.YELLOW + "  clear history / 清理历史 / -ch: 清除历史记录" + Style.RESET_ALL)
            continue

        assistant_response = get_response(user_input, model_name)
        execute_command(assistant_response)
